package com.onluy.framework.tools;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;



/**
 * DES 加解密 实体类，供 DesUtils 调用
 * @author xmh
 *
 */
public class DesEncrypt {
	Key key;  
	  
    public DesEncrypt(String str) {  
        setKey(str);// 生成密匙  
    }  
  
    public DesEncrypt() {  
        setKey("e036hlqtculakx38tjw2u9gs");  
    }  
  
    /**  
     * 根据参数生成KEY  
     */  
    public void setKey(String strKey) {  
        try {  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
            this.key  = keyFactory.generateSecret(new DESKeySpec(strKey.getBytes("UTF8")));  
        } catch (Exception e) {  
            throw new RuntimeException(  
                    "Error initializing SqlMap class. Cause: " + e);  
        }  
    }  
  
      
    /**  
     * 加密String明文输入,String密文输出  
     */  
    public String encrypt(String strMing) {  
        byte[] byteMi = null;  
        byte[] byteMing = null; 
        byte[] byteMi64 = null;
        String strMi = "";  
        //BASE64Encoder base64en = new BASE64Encoder();  
        Base64 base64en=new Base64(); 
        try {  
            byteMing = strMing.getBytes("UTF8");  
            byteMi = this.getEncCode(byteMing);  
            //strMi = base64en.encode(byteMi);  
            byteMi64 = base64en.encode(byteMi); 
            strMi = new String(byteMi64);
        } catch (Exception e) {  
            throw new RuntimeException(  
                    "Error initializing SqlMap class. Cause: " + e);  
        } finally {  
            base64en = null;  
            byteMing = null;  
            byteMi = null;  
        }  
        return strMi;  
    }  
  
    /**  
     * 解密 以String密文输入,String明文输出  
     *   
     * @param strMi  
     * @return  
     */  
    public String decrypt(String strMi) {  
        //BASE64Decoder base64De = new BASE64Decoder();  
        Base64 base64De=new Base64(); 
        byte[] byteMing = null;  
        byte[] byteMi = null;  
        String strMing = "";  
        try {  
            //byteMi = base64De.decodeBuffer(strMi);  
        	byteMi = base64De.decode(strMi); 
            byteMing = this.getDesCode(byteMi);  
            strMing = new String(byteMing, "UTF8");  
        } catch (Exception e) {  
            throw new RuntimeException(  
                    "Error initializing SqlMap class. Cause: " + e);  
        } finally {  
            base64De = null;  
            byteMing = null;  
            byteMi = null;  
        }  
        return strMing;  
    }  
  
    /**  
     * 加密以byte[]明文输入,byte[]密文输出  
     *   
     * @param byteS  
     * @return  
     */  
    private byte[] getEncCode(byte[] byteS) {  
        byte[] byteFina = null;  
        Cipher cipher;  
        try {  
            cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.ENCRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));  
            byteFina = cipher.doFinal(byteS);  
        } catch (Exception e) {  
            throw new RuntimeException(  
                    "Error initializing SqlMap class. Cause: " + e);  
        } finally {  
            cipher = null;  
        }  
        return byteFina;  
    }  
  
    /**  
     * 解密以byte[]密文输入,以byte[]明文输出  
     *   
     * @param byteD  
     * @return  
     */  
    private byte[] getDesCode(byte[] byteD) {  
        Cipher cipher;  
        byte[] byteFina = null;  
        try {  
            cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.DECRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));  
            byteFina = cipher.doFinal(byteD);  
        } catch (Exception e) {  
            throw new RuntimeException(  
                    "Error initializing SqlMap class. Cause: " + e);  
        } finally {  
            cipher = null;  
        }  
        return byteFina;  
    }  
  
      
  
    public static void main(String args[])  {  
//        DesEncrypt des = new DesEncrypt();  
//  
//        String str1 = "19";  
//        // DES加密  
//        String str2 = des.encrypt(str1);  
//        DesEncrypt des1 = new DesEncrypt();  
//        String deStr = des1.decrypt(str2);  
//        System.out.println("密文:" + str2);  
//        // DES解密  
//        System.out.println("明文:" + deStr);  
    	
    	DesEncrypt des1 = new DesEncrypt();  
    	String str1 = "{\"_LOGIN_PARAM\":\"9E1D89F43B558A7DDDB7FDD172A0FD97A861F7C5DB1928656BA890B73D9719C70F92C6CDF6B3F0F5FE3585594AA2574D9C823D81CF0C4F49B21D1EE792C45CB1CE81FC3247E45C1E04416B062BFB45C55C33A406CAA3ECDF0DBD5926B26F86751A5B02584BF3DBB16CD165A0F3D2208350A266651E18B421841EC6DB8934C65516C4FA783F1D2DC583C8012CAC6344B7D04E215DDE91D4AB726B017AFD78AB57EDECA1912C1961419A506BE19FEBE980B678D20184AC76531C4D38D971F405F8C1FB0C8889D067C7D1F32771B322F896169541DA02BEB4FDAD7FD2623EA64BCEB372964910AEF760D9F04BC71F0B34CD82F820B27CF11E83D31F1B5C90B8FAFB7DEDC39EF413B118AD7FD2623EA64BCE690A78972AD725D1FC53ABE9810AF562FA4EB4B37710EB1E285A4EC6229C9758257CBC2C0769BBB9D7C581A6D5F800337C20E3262FE49DBB6BD9D7F2986614781D7F05A8B053CE83EC5DCA44863AEF98C8FD281C4299BCC018593739F6ABFE50F8022CE9AF95328E8F587A327868C4C880C4EFE39DB7B4453588CC73A13677FDC27D0AC4F597BC01E67AD714D18F423C73409DDE561AFBF28A3D50375D7CB946297D726B1AA6F1CE390A34633C10C05EA46E61CB8337B5B4337A9FA8E781A3934C4B7A384EE6BA25BE6ADEA6DAB666DFF8F435D68BC4E59047BD01AF7896BA934C54F6DC799C32AADB06FBBB9BC2389911591CE7BAE396ACAB30574700E3001218521D996BC85C2A139D1E2E455E48638E85746B3DEF1CF2E03951D0742D23BE971C6B0646B7523D566591BB0AA4F7DC41D22785E4EFA70EB0E5CB5E228D634B\",\"staffId\":\"NBCS0027\"}";
    	String str3 = des1.encrypt(str1);
    	System.out.println("密文:" + str3); 
    	String Str2 = "QNMmKz+ZBed12ec/rRacBBAWbex6zTBmNBaInzInx64jf4wCsZ7R3hTKoGhulQF7Bd2koJ6smgWTZzvNNWAM0+A/Rn/FTk1/eL7jL8ygA5xX5EwA0/9E7GhUOhkqfIWTCg1Ewrfvos4LL6zfOlVCDi/7N3yGm9kS1pYOGR8RSREWmWwaLxizldkrxrhq3TvxVCss/GPifDMK0OkCdSl7/nOfrriL4NB6xfNmiXIAJa/yvFnQuK3+AeRlGRKN1tZFv+paeWU84gATJMhuhUBE7IFVd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